package com.jiaeda.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by chengchaopeng on 2018/5/26.
 */
public class CustomRealm extends AuthorizingRealm {

    Map<String, String> userMap = new HashMap<>(16);

    {
        userMap.put("Geoccp", "8ba7d14c8f97c5c69e391ed44dbd37ac");
        super.setName("custonRealm");
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //1、从主体传递过来的认证信息获取用户名
        String userName = (String) principalCollection.getPrimaryPrincipal();
        //从数据库或者缓存中获取角色数据
        Set<String> roles = getRolesByUserName(userName);

        Set<String> permissions = getPermissionsByUserName(userName);
        SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
        //设置角色信息
        authorizationInfo.setRoles(roles);
        //设置权限信息
        authorizationInfo.setStringPermissions(permissions);
        return authorizationInfo;
    }

    /**
     * 根据用户名获取权限数据
     * @param userName
     * @return
     */
    private Set<String> getPermissionsByUserName(String userName) {
        Set<String> sets = new HashSet<>();
        sets.add("user:delete");
        sets.add("user:update");
        return sets;
    }

    /**
     * 根据用户名获取角色名
     * @param userName
     * @return
     */
    private Set<String> getRolesByUserName(String userName) {
        Set<String> sets = new HashSet<>();
        sets.add("admin");
        sets.add("user");
        return sets;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1、从主体传递过来的认证信息获取用户名
        String userName = (String) authenticationToken.getPrincipal();
        //2、通过用户名获得密码
        String password = getPasswordByUserName(userName);
        if (password == null)
            return null;
        SimpleAuthenticationInfo authenticationInfo = new
                SimpleAuthenticationInfo("Geoccp", password, "custonRealm");
        //设置加盐常量
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("geoccp"));
        return authenticationInfo;
    }

    /**
     * 模拟数据库
     *
     * @param userName
     * @return
     */
    private String getPasswordByUserName(String userName) {
        return userMap.get(userName);
    }

//    public static void   main(String[] args) {
//        String hashAlgorithmName = "MD5";
//        String credentials = "123456";
//        int hashIterations = 1024;
//        Object obj = new SimpleHash(hashAlgorithmName, credentials, null, hashIterations);
//        System.out.println(obj);
//    }

    public static void main(String[] args) {
        //加密 加盐
        Md5Hash md5Hash=new Md5Hash("123456","geoccp");
        System.out.println(md5Hash.toString());

    }
}
